package com.exp.bookhouse.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.exp.bookhouse.pojo.Book;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class JsoupUtil {

    public static void main(String[] args) throws IOException {
        JsoupJDTest("文学书籍",1L);
    }

    /**
     * 爬取京东书籍
     * @param keyword  分类
     * @param categoryId  分类id
     * @return
     */
    public static List<Book> JsoupJDTest(String keyword, Long categoryId){
        String url = "https://search.jd.com/Search?keyword="+keyword;       //商品分类请求url
        String detailPrefix = "https://item.jd.com/";       //https://item.jd.com/10058164.html     商品详情请求url
        String priceUrlPrefix = "http://p.3.cn/prices/get?skuid=J_";      //商品价格请求url   J_商品id


        Document document = null;       //搜索分类的document
        try {
            document = Jsoup.parse(new URL(url),30000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");


        Document documentDetail = null;     //商品详情页的document
        List<Book> books = new ArrayList<Book>();   //保存书籍列表

        for(int k = 0; k < elements.size(); k++) {
            Element ele = elements.get(k);
            Book book = new Book();
            String skuId = ele.attr("data-sku");
            String urlDetail = detailPrefix + skuId + ".html";
            try {
                documentDetail = Jsoup.parse(new URL(urlDetail),30000);
                //获取书名
                Element e1 = documentDetail.getElementById("crumb-wrap");
                String name = e1.getElementsByClass("item ellipsis").get(0).text();
                book.setName(name);

                //获取作者名字
                Element e2 = documentDetail.getElementById("p-author");
                Elements elements1 = e2.getElementsByTag("a");
                if(elements1 == null || elements1.size() <= 0 || "".equals(elements1)) continue;
                String author = elements1.get(0).text();
                book.setAuthor(author);

                //获取价格
                String priceUrl = priceUrlPrefix + skuId;
                Document doc = Jsoup
                        .connect(priceUrl)
                        .header("Accept", "*/*")
                        .header("Accept-Encoding", "gzip, deflate")
                        .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                        .header("Content-Type", "application/json;charset=UTF-8")
                        .header("MyUser-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
                        .ignoreContentType(true)
                        .timeout(10000).get();
                Element body = doc.body();
                JSONArray objects = JSONArray.parseArray(body.text());
                JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString());
                String priceStr = jsonObject.getString("p");   //价格字符串
                //将价格字符串转为long类型, 并且单位转为 ：分
                BigDecimal temp = BigDecimal.valueOf(Double.valueOf(priceStr));
                temp = temp.multiply(BigDecimal.valueOf(100));  // 将temp乘以100， 转为：分
                Long price = temp.longValue();
                book.setPrice(price);

                //获取描述
                Elements eles = documentDetail.getElementsByTag("head");
                for (Element el : eles) {
                    for (Element meta : el.getElementsByTag("meta")) {
                        if("description".equals(meta.getElementsByTag("meta").attr("name"))){
                            String description = meta.attr("content");
                            book.setDescription(description);
                            break;
                        }
                    }
                }

                //获取出版社
                Element el4 = documentDetail.getElementById("detail").getElementById("parameter2");
                for (Element li : el4.getElementsByTag("li")) {
                    String title = li.attr("title");
                    if(title.contains("出版社")){
                        String publishHouse = li.getElementsByTag("a").get(0).text();
                        book.setPublishHouse(publishHouse);
                        break;
                    }
                }

                //获取大图片地址，最多3个和小图片地址
                Element el5 = documentDetail.getElementById("spec-list");
                Element element2 = el5.getElementsByClass("lh").first();
                Elements lis = element2.getElementsByTag("li");
                StringBuilder pic = new StringBuilder();
                for (int i = 0; i < lis.size(); i++) {
                    Element li = lis.get(i);
                    String image = li.getElementsByTag("img").first().attr("src");
                    if(i == 0){
                        book.setIcon(image);
                    }
                    if(i == lis.size() - 1 || i == 2){
                        pic.append(image);
                        break;
                    }else{
                        pic.append(image+",");
                    }
                }
                book.setPic(pic.toString());

                //设置库存
                book.setStock(100);

                //设置是否推荐，每隔10，一个推荐
                if(k%10 == 0){
                    book.setRecommend(1);
                }else{
                    book.setRecommend(0);
                }

                //设置分类id
                book.setCategoryId(categoryId);

                System.out.println(book);
                books.add(book);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("数目："+books.size());
        return books;
    }


}
